#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : function.py
# Author    : myh

from frame_class import Frame
from pathlib import Path


# 导入文件数据
# real_path:需要导入文件的真实路径
def f_data_in(real_path):
    path = Path(real_path)
    contents = path.read_text()
    return contents


# 某一条信息的时间解析
# line:用于解析的某个字符串
# 返回值：minute:分 ； sec:秒 ； msec:毫秒
def analysis_time(line):
    index = line.find('I 1970-01-01-00:')
    len_base = index + len('I 1970-01-01-00:')
    minute = int(line[len_base: len_base + 2])
    sec = int(line[len_base + 3: len_base + 5])
    msec = int(line[len_base + 6: len_base + 12])
    return [minute, sec, msec]


# 解析某种属性
# line:用于解析的某个字符串
# attribute_name:需要解析的属性名（字符串格式）
# int(attribute_value) 返回属性的数值
def analysis_attribute(line, attribute_name):
    index = line.find(attribute_name)
    # attribute_len = 0
    attribute_value = ''
    for string in line[index + len(attribute_name):]:
        if string.isdigit():
            attribute_value += string
        else:
            break

    return int(attribute_value)


# 选择赋值不同的属性
# line:用于解析的某个字符串
# old_index：当前id在id库中的位置
# id_infos：id库
# 返回pA与pB的数量增加量a_num_add与b_num_add

def attribute_choose(line, old_index, id_infos):
    a_num_add = 0
    b_num_add = 0
    if 'ProcessA:start:' in line:
        attribute_value = analysis_attribute(line, 'ProcessA:start:')
        id_infos[old_index].pa_start_time = attribute_value
        id_infos[old_index].astart_time = analysis_time(line)
    elif 'ProcessA:end:' in line:
        attribute_value = analysis_attribute(line, 'ProcessA:end:')
        id_infos[old_index].pa_end_time = attribute_value
        a_num_add = 1
    elif 'ProcessB:start:' in line:
        attribute_value = analysis_attribute(line, 'ProcessB:start:')
        id_infos[old_index].pb_start_time = attribute_value
    elif 'ProcessB:end:' in line:
        attribute_value = analysis_attribute(line, 'ProcessB:end:')
        id_infos[old_index].pb_end_time = attribute_value
        b_num_add = 1
        id_infos[old_index].bend_time = analysis_time(line)
    return [a_num_add,b_num_add]


# 解析文件数据
def analysis_file(contents):
    lines = contents.splitlines()
    id_infos = []
    old_ids = []
    a_num = 0
    b_num = 0
    min1 = 0
    sec1 = 0
    msec1 = 0
    min2 = 0
    sec2 =0
    msec2 = 0
    flag1 = True
    for line in lines:
        if 'static_multistream' in line:
            if 'I 1970-01-01-00:' in line:
                if flag1:
                    flag1 = False
                    [min1, sec1, msec1] = analysis_time(line)
                    print([min1, sec1, msec1])
            # 读取frame_id
            if 'frame_id:' in line:
                frame_id = analysis_attribute(line, 'frame_id:')
                if frame_id not in old_ids:
                    new_frame = Frame(frame_id)
                    id_infos.append(new_frame)
                    old_ids.append(frame_id)

                old_index = old_ids.index(frame_id)
                [a_num_add, b_num_add] = attribute_choose(line, old_index, id_infos)
                a_num += a_num_add
                b_num += b_num_add
                if 'I 1970-01-01-00:' in line:
                    [min2, sec2, msec2] = analysis_time(line)

    all_time = (min2 - min1)*60 + (sec2 - sec1) + (msec2-msec1)/1000000
    print(all_time)
    print(a_num)
    print(b_num)
    return [id_infos,a_num,b_num,all_time]




